#objc code snippet code
Explore tagged Tumblr posts
Text
[OBJ-C] set Frame not working
[view setFrame:CGRect(0,0,200,300)]; If the above line is not doing what you want it’s probably because the parent view is autolaying out the child views. To manually setup the frame of a view you can: Set the view after the layout:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
//ADD YOUR CODE TO SET THE FRAME MANUALLY
//for example:
[view setFrame:CGRectMake(0, 0, 200, 200)]; }
Or you can disable the auto layout of a single view: view.translatesAutoresizingMaskIntoConstraints = YES;
1 note
·
View note
Text
Swift Keyboard Control for TextFields and TextViews
Keyboard Control as User Navigates TextFields and TextViews on Table View Form
Scenario
Our app will allow users to tap between TextFields and TextViews and control the keyboard display (hide or show) as required. The code will ensure that the keyboard does not override (hide) the TextField or TextView the user has in focus.
Technique
We shall utilise the Delegate feature so the view, as the delegate to the TextFields and TextViews, can control the keyboard display. Then we shall use GestureRecognizer to detect user gestures and check the TextField or TextView BeginEditing and EndEditing conditions and animate the fields or views to move them up or down, depending on the keyboard display status.
Audience
The article is for Swift developers who seek complete, integrated, proven, code-centric solutions to speed up their development projects.
App Model
We based the article on the AppsGym Books model app, published on Apple’s App Store (as 8Books), and you can download the complete Xcode project on AppsGym.com, for free.
User Interfaces
NewBookTableViewController.swift and EditBookTableViewController.swift control the layout, order, and navigation of the corresponding UITableViewControllers.
Logic
NewBookTableViewController.swift contains keyboard control logic, which monitors and detects user taps on the form fields and reacts accordingly.
There are 7 TextFields on the New Book view: Title, Author, Series, No in Series, Category (Genre), Source, and Format. Each field has a Tag on the Storyboard (e.g., titleTextField.tag = 1) and each behaviour is governed by 3 textField functions, which we shall utilise to control the textField attributes and movement by controlling the becomeFirstResponder() function.
textFieldShouldReturn(..), textFieldDidBeginEditing(..), and textFieldDidEndEditing(..)
There are 2 TextViews on the New Book view: Synopsis and Notes. We shall use 2 textView functions to control their behaviour: textViewDidBeginEditing (..) and textViewDidEndEditing(..).
Code
We shall separate the code of TextFields and TextView to make it easier to focus on each group keyboard control.
TextFields
NewBookTableViewController.swift Initialise Variables
// Tags are used to Auto Jump to next TextField; see func textFieldShouldReturn @IBOutlet weak var titleTextField: UITextField! { didSet { titleTextField.tag = 1 titleTextField.delegate = self } } @IBOutlet weak var authorTextField: UITextField! { didSet { authorTextField.tag = 2 authorTextField.delegate = self } } ... @IBOutlet weak var formatTextField: UITextField! { didSet { formatTextField.tag = 7 formatTextField.delegate = self } } ....
NewBookTableViewController.swift viewDidLoad()
... let tapGestureReconizer = UITapGestureRecognizer(target: self, action: #selector(NewBookTableViewController.tap(_:))) view.addGestureRecognizer(tapGestureReconizer) ... }
NewBookTableViewController.swift Keyboard Control
func textFieldDidBeginEditing(_ textField: UITextField) { textField.layer.borderWidth = 2 textField.layer.borderColor = UIColor.orange.cgColor } func textFieldDidEndEditing(_ textField: UITextField) { textField.layer.borderWidth = 0 textField.layer.borderColor = UIColor.clear.cgColor }
@objc func tap(_ sender: UITapGestureRecognizer) { view.endEditing(true) } view raw
TextViews
NewBookTableViewController.swift Initialise Variables
class NewBookTableViewController: UITableViewController, UITextViewDelegate { ... @IBOutlet weak var synopsisTextView: UITextView! @IBOutlet weak var notesTextView: UITextView! ...
ewBookTableViewController.swift viewDidLoad()
override func viewDidLoad() { super.viewDidLoad() ... self.synopsisTextView.delegate = self // keyboard control self.notesTextView.delegate = self // keyboard control synopsisTextView.text = "" notesTextView.text = "" // stringCurrentDate() + "nn" ... let tapGestureReconizer = UITapGestureRecognizer(target: self, action: #selector(NewBookTableViewController.tap(_:))) view.addGestureRecognizer(tapGestureReconizer) ... } // end viewDidLoad()
NewBookTableViewController.swift** Keyboard Control**
/ 4 funcs To dimiss keyboard and move the textView UP/Down when the keyboard shows @objc func tap(_ sender: UITapGestureRecognizer) { view.endEditing(true) } func textViewDidBeginEditing(_ textView: UITextView) { textView.layer.borderWidth = 2 textView.layer.borderColor = UIColor.orange.cgColor animateViewMoving(true, moveValue: 100) } func textViewDidEndEditing(_ textView: UITextView) { textView.layer.borderWidth = 0 textView.layer.borderColor = UIColor.clear.cgColor animateViewMoving(false, moveValue: 100) } func animateViewMoving (_ up:Bool, moveValue :CGFloat){ let movementDuration:TimeInterval = 0.3 let movement:CGFloat = ( up ? -moveValue : moveValue) UIView.beginAnimations( "animateView", context: nil) UIView.setAnimationBeginsFromCurrentState(true) UIView.setAnimationDuration(movementDuration ) self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement) UIView.commitAnimations() }
**
**
Keyboard Height
The code snippets above used a simple moveValue of 100, which would suffice for most scenarios. However, a more accurate measure would be the actual keyboard height on the device.
The code snippet below shows how to obtain the Keyboard Height. If you want to use this method, substitute the keyboardHeight for the ‘100’ in the moveValue argument for TextView. You can optionally use keyboardHeight in the TextField functions for more precise controls.
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { keyboardHeight = keyboardSize.height print(#function, keyboardHeight)
// The 1st keyboardWillShow gets the keyboard size height then observer removed as no need to get keyboard height every time it shows or hides
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil) // Store KeyboardHeight in UserDefaults to use when in Edit Mode
UserDefaults.standard.set(keyboardHeight, forKey: "KeyboardHeight") UserDefaults.standard.synchronize() } } // end func keyboardWillShow
The article covered the complete logic and code to control the behaviour of TextFields and TextViews, so the keyboard does not obscure the relevant fields or views behind it. Hope you find it useful in your app. Thanks for reading!
0 notes
Text
Flutter is a new mobile app SDK to help developers and designers build modern mobile apps for iOS and Android.
Features of Flutter:-
1. Fast development
Flutter’s hot reload helps you quickly and easily experiment, build UIs, add features, and fix bug faster. Experience sub-second reload times, without losing state, on emulators, simulators, and hardware for iOS and Android.
2. Expressive, beautiful UIs
Delight your users with Flutter’s built-in beautiful Material Design and Cupertino (iOS-flavor) widgets, rich motion APIs, smooth natural scrolling, and platform awareness.
3. Modern, reactive framework
Easily compose your UI with Flutter’s modern reactive framework and the rich set of the platform, layout, and foundation widgets. Solve your tough UI challenges with powerful and flexible APIs for 2D, animation, gestures, effects, and more.
class CounterState extends State<Counter> { int counter = 0; void increment() { // Tells the Flutter framework that state has changed, // so the framework can run build() and update the display. setState(() { counter++; }); } Widget build(BuildContext context) { // This method is rerun every time setState is called. // The Flutter framework has been optimized to make rerunning // build methods fast, so that you can just rebuild anything that // needs updating rather than having to individually change // instances of widgets. return new Row( children: <Widget>[ new RaisedButton( onPressed: increment, child: new Text('Increment'), ), new Text('Count: $counter'), ], ); } }
4.Access native features and SDKs
Make your app come to life with platform APIs, 3rd party SDKs, and native code. Flutter lets you reuse your existing Java, Swift, and ObjC code, and access native features and SDKs on iOS and Android.
Accessing platform features is easy. Here is a snippet from our interop example:
Future<Null> getBatteryLevel() async { var batteryLevel = 'unknown'; try { int result = await methodChannel.invokeMethod('getBatteryLevel'); batteryLevel = 'Battery level: $result%'; } on PlatformException { batteryLevel = 'Failed to get battery level.'; } setState(() { _batteryLevel = batteryLevel; }); }
5.Unified app development
Flutter has the tools and libraries to help you easily bring your ideas to life on iOS and Android. If you don’t have any mobile development experience, Flutter is an easy and fast way to build beautiful mobile apps. If you are an experienced iOS or Android developer, you can use Flutter for your views and leverage much of your existing Java/ObjC/Swift investment.
Flutter: Easy and fast SDK for beautiful Apps Development Flutter is a new mobile app SDK to help developers and designers build modern mobile apps for iOS and Android.
0 notes
Text
Flutter is a new mobile app SDK to help developers and designers build modern mobile apps for iOS and Android.
Features of Flutter:-
1. Fast development
Flutter’s hot reload helps you quickly and easily experiment, build UIs, add features, and fix bug faster. Experience sub-second reload times, without losing state, on emulators, simulators, and hardware for iOS and Android.
2. Expressive, beautiful UIs
Delight your users with Flutter’s built-in beautiful Material Design and Cupertino (iOS-flavor) widgets, rich motion APIs, smooth natural scrolling, and platform awareness.
3. Modern, reactive framework
Easily compose your UI with Flutter’s modern reactive framework and the rich set of the platform, layout, and foundation widgets. Solve your tough UI challenges with powerful and flexible APIs for 2D, animation, gestures, effects, and more.
class CounterState extends State<Counter> { int counter = 0; void increment() { // Tells the Flutter framework that state has changed, // so the framework can run build() and update the display. setState(() { counter++; }); } Widget build(BuildContext context) { // This method is rerun every time setState is called. // The Flutter framework has been optimized to make rerunning // build methods fast, so that you can just rebuild anything that // needs updating rather than having to individually change // instances of widgets. return new Row( children: <Widget>[ new RaisedButton( onPressed: increment, child: new Text('Increment'), ), new Text('Count: $counter'), ], ); } }
4.Access native features and SDKs
Make your app come to life with platform APIs, 3rd party SDKs, and native code. Flutter lets you reuse your existing Java, Swift, and ObjC code, and access native features and SDKs on iOS and Android.
Accessing platform features is easy. Here is a snippet from our interop example:
Future<Null> getBatteryLevel() async { var batteryLevel = 'unknown'; try { int result = await methodChannel.invokeMethod('getBatteryLevel'); batteryLevel = 'Battery level: $result%'; } on PlatformException { batteryLevel = 'Failed to get battery level.'; } setState(() { _batteryLevel = batteryLevel; }); }
5.Unified app development
Flutter has the tools and libraries to help you easily bring your ideas to life on iOS and Android. If you don’t have any mobile development experience, Flutter is an easy and fast way to build beautiful mobile apps. If you are an experienced iOS or Android developer, you can use Flutter for your views and leverage much of your existing Java/ObjC/Swift investment.
Flutter: Easy and fast SDK for beautiful Apps Development Flutter is a new mobile app SDK to help developers and designers build modern mobile apps for iOS and Android.
0 notes